package com.lhz.common.page;


import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import lombok.Getter;

import java.util.List;

/**
 * @Author: LiHuaZhi
 * @Date: 2021/11/19 11:27
 * @Version: 1.0
 **/
@Getter
public class NewPageBean<T> {
    private Long total;
    private List<T> list;
    private MyPage pageInfo;

    /**
     * @param select 调用Mapper接口的结果
     * @param param  请求参数
     * @param tClass 响应的Class对象
     * @param <T>
     * @return
     */
    public static <T> NewPageBean<T> generatePage(ISelect select, BaseQueryParam param, Class<T> tClass) {
        Integer current = param.getPageNum();
        Integer pageSize = param.getPageSize();
        List<T> list = PageHelper
                .startPage(current == null ? 1 : current, pageSize == null ? 10 : pageSize)
                .doSelectPage(select);
        return new NewPageBean<>(list);
    }

    public NewPageBean(List<T> list) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        this.total = pageInfo.getTotal();
        this.list = list;
        MyPage page = new MyPage();
        page.setTotalPage(pageInfo.getPages());
        page.setPageNum(pageInfo.getPageNum());
        page.setPageSize(pageInfo.getPageSize());
        page.setPreviousPage(pageInfo.getPrePage());
        page.setNextPage(pageInfo.getNextPage());
        this.pageInfo = page;
    }

    public long getNum() {
        return this.total;
    }

    public void setNum(Long total) {
        this.total = total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public void setPageInfo(MyPage pageInfo) {
        this.pageInfo = pageInfo;
    }

    @Data
    static class MyPage {
        private int pageNum;
        private int pageSize;
        private int totalPage;
        private int nextPage;
        private int previousPage;

    }
}
